package com.yuhang.demo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yuhang.demo.dto.request.ConditionAddRequest;
import com.yuhang.demo.dto.request.ConditionUpdateRequest;
import com.yuhang.demo.dto.response.Result;
import com.yuhang.demo.entity.RuleCondition;
import com.yuhang.demo.entity.RuleInfo;
import com.yuhang.demo.mapper.RuleConditionMapper;
import com.yuhang.demo.service.RuleConditionService;
import com.yuhang.demo.service.RuleInfoService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class RuleConditionServiceImpl extends ServiceImpl<RuleConditionMapper, RuleCondition> implements RuleConditionService {

    @Autowired
    private RuleConditionMapper conditionMapper;

    @Lazy
    @Autowired
    private RuleInfoService ruleInfoService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Result<Long> addCondition(ConditionAddRequest request) {
        // 1. 校验规则是否存在
        Result<RuleInfo> ruleResult = ruleInfoService.getRuleById(request.getRuleId());
        if (ruleResult.getCode() != 200) {
            return Result.fail(404, "所属规则不存在");
        }

        // 2. 封装条件实体
        RuleCondition condition = new RuleCondition();
        BeanUtils.copyProperties(request, condition);

        // 3. 插入数据库
        conditionMapper.insert(condition);
        return Result.success(condition.getId());
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Result<Boolean> updateCondition(ConditionUpdateRequest request) {
        // 1. 校验条件是否存在
        RuleCondition existCondition = conditionMapper.selectById(request.getId());
        if (existCondition == null) {
            return Result.fail(404, "条件不存在");
        }

        // 2. 封装更新数据
        RuleCondition updateCondition = new RuleCondition();
        BeanUtils.copyProperties(request, updateCondition);

        // 3. 执行更新
        conditionMapper.updateById(updateCondition);
        return Result.success(true);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Result<Boolean> deleteCondition(Long id) {
        // 1. 校验条件是否存在
        RuleCondition existCondition = conditionMapper.selectById(id);
        if (existCondition == null) {
            return Result.fail(404, "条件不存在");
        }

        // 2. 逻辑删除条件
        conditionMapper.deleteById(id);
        return Result.success(true);
    }

    @Override
    public Result<List<RuleCondition>> getConditionByRuleId(Long ruleId) {
        // 1. 校验规则是否存在
        Result<RuleInfo> ruleResult = ruleInfoService.getRuleById(ruleId);
        if (ruleResult.getCode() != 200) {
            return Result.fail(404, "所属规则不存在");
        }

        // 2. 查询条件列表
        List<RuleCondition> conditionList = conditionMapper.selectByRuleId(ruleId);
        return Result.success(conditionList);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteByRuleId(Long ruleId) {
        // 批量删除指定规则的所有条件
        LambdaQueryWrapper<RuleCondition> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(RuleCondition::getRuleId, ruleId);
        conditionMapper.delete(wrapper);
    }
}
